package com.snail.sell.controller;

import com.snail.sell.Exception.SellException;
import com.snail.sell.Form.CategoryForm;
import com.snail.sell.dataobject.ProductCategory;
import com.snail.sell.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author: XJL
 * @Description:
 * @Date: Create in 18:57 2017/12/18
 * @Modified By:
 **/
@Controller
@RequestMapping("/seller/category")
public class SellerCategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查找类目列表
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(Map<String,Object> map){


            List<ProductCategory> productCategoryList = categoryService.findAll();
            map.put("productCategoryList",productCategoryList);
            return new ModelAndView("category/list",map);


    }


    /**
     * 类目详情
     * @param categoryId
     * @param map
     * @return
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId",required = false) Integer categoryId,
                              Map<String,Object> map){
        ProductCategory productCategory = new ProductCategory();
        if (categoryId!=null){
            try {
                productCategory = categoryService.findOne(categoryId);
                map.put("productCategory",productCategory);
            }catch (SellException e){
                map.put("msg",e.getMessage());
                map.put("url","/sell/seller/category/list");
            }

        }
       return new ModelAndView("category/index",map);
    }

    @PostMapping("/save")
    public ModelAndView save(@Valid CategoryForm categoryForm,
                             BindingResult bindingResult,
                             Map<String,Object> map){
        if (bindingResult.hasErrors()){
            map.put("msg",bindingResult.getFieldError().getDefaultMessage());
            map.put("url","/sell/seller/category/index");
            return new ModelAndView("common/error",map);
        }
        ProductCategory productCategory = new ProductCategory();
        try {
            if (categoryForm.getCategoryId()!=null){
                productCategory = categoryService.findOne(categoryForm.getCategoryId());


            }
            BeanUtils.copyProperties(categoryForm,productCategory);
            categoryService.save(productCategory);
        }catch (SellException e){
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/category/index");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/sell/seller/category/list");
        return new ModelAndView("common/success",map);
    }
}
